package com.bluedot.bank.dao;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;


public class OpenSessionInViewFilter implements Filter {

	@Override
	public void destroy() {
		HibernateDaoSupport.close();

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HibernateDaoSupport.initSession();
		try {
			chain.doFilter(request, response);
			HibernateDaoSupport.commit();
		} catch (RuntimeException e) {
			HibernateDaoSupport.rollback();
			throw e;
		} finally {
			HibernateDaoSupport.close();
		}

	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
